﻿using Lima.ResumeSys.Class;
using Lima.ResumeSys.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Lima.ResumeSys.Models.PerFile;
using static Lima.ResumeSys.Models.RewardPunish;

namespace Lima.ResumeSys.DAL
{
    public class PerFileDAL
    {
        private clsDbOperate dbOperate = new clsDbOperate();
        public PerFile ConvertToModel(DataRow row)
        {
            PerFile model = new PerFile();
            if (!string.IsNullOrEmpty(row["PerFileId"].ToString()))
            {
                model.PerFileId = row["PerFileId"].ToString();
            }

            if (!string.IsNullOrEmpty(row["PerNo"].ToString()))
            {
                model.PerNo = row["PerNo"].ToString();
            }

            if (!string.IsNullOrEmpty(row["FileName"].ToString()))
            {
                model.FileName = row["FileName"].ToString();
            }
            if (!string.IsNullOrEmpty(row["FileSize"].ToString()))
            {

                model.FileSize = row["FileSize"].ToString();
            }


            // 转换长文本字段
            if (!string.IsNullOrEmpty(row["FileContent"].ToString()))
            {
                model.FileContent = row["FileContent"].ToString();
            }

            if (!string.IsNullOrEmpty(row["Creator"].ToString()))
            {
                model.Creator = row["Creator"].ToString();
            }

            // 转换日期/时间字段
            if (!string.IsNullOrEmpty(row["CreateTime"].ToString()))
            {
                model.CreateTime = Convert.ToDateTime(row["CreateTime"]);
            }

            return model;
        }

        public PerFile GetModel(string perFileId)
        {
            try
            {
                string sql = @"select  PerFileId,PerNo,FileName,FileContent,FileSize,Creator,CreateTime from PerFile where PerFileId=@perFileId ";
                OleDbParameter[] parameters = { new OleDbParameter("@perFileId", perFileId) };
                DataTable dt = dbOperate.ExecuteQuery(sql, parameters);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    return ConvertToModel(row);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public bool Add(PerFile model)
        {
            try
            {
                string sql = @"INSERT INTO PerFile(
                PerFileId,PerNo,FileName,FileContent,FileSize,CreateTime,Creator ) VALUES (
                @PerFileId,@PerNo,@FileName,@FileContent,@FileSize,@CreateTime,@Creator
            )"
                ;

                OleDbParameter[] parameters =
                    {
            new OleDbParameter("@PerFileId", model.PerFileId),
            new OleDbParameter("@PerNo", model.PerNo),
            new OleDbParameter("@FileName", model.FileName),
            new OleDbParameter("@FileContent", Convert.ToBase64String(File.ReadAllBytes(model.FileUrl))),
            new OleDbParameter("@FileSize", model.FileSize),
            new OleDbParameter("@CreateTime", DateTime.Now.ToString()),
            new OleDbParameter("@Creator", model.Creator)
            
            };

                int result = dbOperate.ExecuteNonQuery(sql, parameters);
                return result > 0;
            }
            catch
            {
                return false;
            }
        }
        public bool Del(string perFileId)
        {
            try
            {
                string sql = @"delete from PerFile where PerFileId=@perFileId ";
                OleDbParameter[] parameters = { new OleDbParameter("@perFileId", perFileId) };
                int result = dbOperate.ExecuteNonQuery(sql, parameters);
                return result > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public List<PerFile> GetList(QueryPerFileModel query)
        {
            string strWhere = "";
            List<OleDbParameter> parameters = new List<OleDbParameter>();

            if (!string.IsNullOrWhiteSpace(query.PerNo))
            {
                strWhere += " and PerNo = @PerNo ";
                parameters.Add(new OleDbParameter("@PerNo", query.PerNo));
            }

            string sql = "SELECT PerFileId,PerNo,FileName,'' as FileContent ,FileSize,Creator,CreateTime FROM PerFile WHERE 1=1 " + strWhere +
                        " ORDER BY CreateTime DESC ";
            List<PerFile> list = new List<PerFile>();
            //获取数据列表
            var dataTables = dbOperate.ExecuteQuery(sql, parameters.ToArray());

            if (dataTables != null && dataTables.Rows.Count > 0)
            {
                foreach (DataRow row in dataTables.Rows)
                {
                    list.Add(ConvertToModel(row));
                }
            }
            return list;
        }

    }
}
